package com.sgcc.pda.mdrh.receiver;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.sgcc.pda.baselibrary.utils.LogUtil;
//import com.sgcc.pda.mdrh.ui.activity.SystemInitActivity;


/**
 * App 退出广播
 *
 * @author Ben
 * @date 2019/05/09
 */
public class SystemExitReceiver extends BroadcastReceiver {

    private static final String ACTION = "com.sgcc.pda.SystemExitReceiver";

    private SystemExitReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            LogUtil.d("Ben action : " + action);
            if (TextUtils.equals(action, ACTION)) {
                //TODO restart(context, SystemInitActivity.class);
            }
        }
    }


    /**
     * 注册广播
     *
     * @param context
     */
    private void register(@NonNull Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION);
        context.registerReceiver(this, filter);
    }

    /**
     * 解除广播
     *
     * @param context
     */
    private void unregister(Context context) {
        context.unregisterReceiver(this);
    }

    /**
     * 重新启动APP
     *
     * @param context
     * @param activity 启动的页面activity
     */
    public void restart(@NonNull Context context, Class<? extends Activity> activity) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null && activity != null) {
            Intent intent = new Intent(context, activity);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("restart", true);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            // 1秒钟后重启应用
            alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);
        }
        systemExit(context);
    }

    /**
     * 系统退出
     *
     * @param context
     */
    private void systemExit(@NonNull Context context) {
        if (mListener != null) {
            mListener.onSystemExit();
        }
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            // 杀死后台进程需要权限 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
            manager.killBackgroundProcesses(context.getPackageName());
        }
        // 杀死进程
        android.os.Process.killProcess(android.os.Process.myPid());
        // 系统退出
        // System.exit(0);
    }

    /**
     * 注册广播接收器
     *
     * @param context
     * @return SystemExitReceiver
     */
    public static SystemExitReceiver registerReceiver(@NonNull Context context) {
        SystemExitReceiver receiver = new SystemExitReceiver();
        receiver.register(context);
        return receiver;
    }

    /**
     * 解除注册广播接收器
     *
     * @param context
     * @param receiver SystemExitReceiver
     */
    public static void unregisterReceiver(@NonNull Context context, SystemExitReceiver receiver) {
        if (receiver != null) {
            receiver.unregister(context);
        }
    }

    /**
     * 发送系统退出广播
     *
     * @param context
     */
    public static void sendBroadcast(@NonNull Context context) {
        context.sendBroadcast(new Intent(ACTION));
    }

    private SystemExitListener mListener;

    public void setSystemExitListener(SystemExitListener listener) {
        this.mListener = listener;
    }

    public interface SystemExitListener {
        void onSystemExit();
    }

}
